package weaver.interfaces.zhlx.duss.action.purchasemodule.wms;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.WmsUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 冻结/解冻流程推送OATR_OA预留同步接口
 * @ModifyLog:
 **/
public class WeaDjJdlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        int formid = Math.abs(requestInfo.getRequestManager().getFormid());
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaDjJdlcAction>>>>>requestid=" + requestid);
        String type = "OATR";
        try {
            String message = "调用WMS系统接口返回:";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            JSONObject dataJson = new JSONObject();
            JSONObject dataheaderJson = new JSONObject();
            dataheaderJson.put("SERIAL", mainid);
            //OA单号
            dataheaderJson.put("RSNUM", Util.null2String(rs.getString("lcbh")));
            //移动类型
            String zylx = Util.null2String(rs.getString("zylx"));
            String ydlxlname = WmsUtil.getSelectItem(formid, "zylx", zylx);
            dataheaderJson.put("BWART", ydlxlname);
            dataJson.put("DATAHEADER", dataheaderJson);
            JSONArray databodyArry = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            int count = 1;
            while (rs.next()) {
                JSONObject databodyJson = new JSONObject();
                //行项目号
                databodyJson.put("RSPOS", count);
                //工厂
                databodyJson.put("WERKS", Util.null2String(rs.getString("gc")));
                //库存地点
                databodyJson.put("LGORT", Util.null2String(rs.getString("kw")));
                //物料号
                databodyJson.put("MATNR", Util.null2String(rs.getString("ljbh")));
                //批次号
                databodyJson.put("CHARG", Util.null2String(rs.getString("ph")));
                //库存跟踪号
                databodyJson.put("KZCODE", Util.null2String(rs.getString("kcgzh")));
                //执行数量
                databodyJson.put("BDMNG", Util.null2String(rs.getString("sl")));
                //基本计量单位
                databodyJson.put("MEINS", Util.null2String(rs.getString("dwsl")));
                //TO物料号
                databodyJson.put("TOMATNR", Util.null2String(rs.getString("towlh")));
                //原因描述
                databodyJson.put("REASON", Util.null2String(rs.getString("yyms")));
                databodyArry.add(databodyJson);
                count++;
            }

            dataJson.put("DATABODY", databodyArry);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaDjJdlcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaDjJdlcAction>>>>>dataJson=" + dataJson);
            JSONObject resultJson = WmsUtil.handleOKHttpRequest(type, dataJson, dataMap);
            writeLog(">>>>>WeaDjJdlcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                if (!"S".equalsIgnoreCase(msgType)) {
                    message += outputJson.getString("MSG_TEXT");
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaDjJdlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}